//
//  PopupView.m
//  买布易
//
//  Created by 张建 on 15/6/26.
//  Copyright (c) 2015年 张建. All rights reserved.
//

#import "AddressPickView.h"
#import "UIViewExt.h"

#define kScreenWidth  [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

#define windowColor  [UIColor colorWithRed:0 green:0 blue:0 alpha:0.2]
#define navigationViewHeight 44.0f
#define pickViewViewHeight 300.0f
#define buttonWidth 60.0f
@interface AddressPickView ()
{


        NSMutableArray *provinceArray;
        NSMutableArray *provinceCodeArray;
        NSMutableArray *cityArray;
        NSMutableArray *cityCodeArray;
        NSMutableArray *townArray;
        NSMutableArray *townCodeArray;
        NSArray *obj ;
        NSMutableArray *newcityArray;
    
        NSMutableArray *_towArray;
        NSMutableArray *_oneArray;
    
    
    
}



@end

@implementation AddressPickView
+ (instancetype)shareInstance
{
    static AddressPickView *shareInstance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareInstance = [[AddressPickView alloc] init];
    });
    
    [shareInstance showBottomView];
    return shareInstance;
}
-(instancetype)init
{
    self = [super init];
    if (self) {
        self.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight);
        [self _addTapGestureRecognizerToSelf];
        [self _getPickerData];
        [self _createView];
    }
    return self;
  
}
#pragma mark - get data
- (void)_getPickerData
{
    
    provinceArray     = [NSMutableArray arrayWithCapacity:100];
    provinceCodeArray = [NSMutableArray arrayWithCapacity:100];
    cityArray         = [NSMutableArray arrayWithCapacity:100];
    cityCodeArray     = [NSMutableArray arrayWithCapacity:100];
    townArray         = [NSMutableArray arrayWithCapacity:100];
    townCodeArray     = [NSMutableArray arrayWithCapacity:100];
    _towArray = [NSMutableArray arrayWithCapacity:100];
    _oneArray = [NSMutableArray arrayWithCapacity:100];
    NSString *path = [[NSBundle mainBundle]pathForResource:@"json" ofType:@"json"];
    NSData *data = [NSData dataWithContentsOfFile:path];
    //用系统自带库进行解析
      obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
    for (int i = 0; i < obj.count; i++) {
       
        NSDictionary  *dic = obj[i];
        [provinceArray addObject:dic[@"text"]];
        [provinceCodeArray addObject:dic[@"value"]];
        NSArray *arr1 = dic[@"children"];
        for (int i = 0; i< arr1.count; i++) {
            NSDictionary *dic1 = arr1[i];
            [cityArray addObject:dic1[@"text"]];
            [cityCodeArray addObject:dic1[@"value"]];
            NSArray *arr2 = dic1[@"children"];
            for (int i = 0; i <arr2.count; i++) {
                NSDictionary *dic2 = arr2[i];
                [townArray addObject:dic2[@"text"]];
                [townCodeArray addObject:dic2[@"value"]];
            }
           
        }
   
    }

    
}
-(void)_addTapGestureRecognizerToSelf
{
    self.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hiddenBottomView)];
    [self addGestureRecognizer:tap];
}
-(void)_createView
{
    
    _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, navigationViewHeight+pickViewViewHeight)];
    [self addSubview:_bottomView];
    //导航视图
    _navigationView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, navigationViewHeight)];
    _navigationView.backgroundColor = [UIColor lightGrayColor];
    [_bottomView addSubview:_navigationView];
    //这里添加空手势不然点击navigationView也会隐藏,
    UITapGestureRecognizer *tapNavigationView = [[UITapGestureRecognizer alloc]initWithTarget:self action:nil];
    [_navigationView addGestureRecognizer:tapNavigationView];
    NSArray *buttonTitleArray = @[@"取消",@"确定"];
    for (int i = 0; i <buttonTitleArray.count ; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(i*(kScreenWidth-buttonWidth), 0, buttonWidth, navigationViewHeight);
        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        [_navigationView addSubview:button];
        
        button.tag = i;
        [button addTarget:self action:@selector(tapButton:) forControlEvents:UIControlEventTouchUpInside];
        

    }
    _pickView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, navigationViewHeight, kScreenWidth, pickViewViewHeight)];
    _pickView.backgroundColor = [UIColor whiteColor];
    _pickView.dataSource = self;
    _pickView.delegate =self;
    [_bottomView addSubview:_pickView];
    
    
}
-(void)tapButton:(UIButton*)button
{
    //点击确定回调block
    if (button.tag == 1) {
        NSLog(@"------%ld",(long)[_pickView selectedRowInComponent:0]);
        
        self.province = [provinceArray objectAtIndex:[_pickView selectedRowInComponent:0]];
        NSString *proCode= [provinceCodeArray objectAtIndex:[_pickView selectedRowInComponent:0]];
        NSLog(@"+++++++%@",proCode);
        NSString *city = [cityArray objectAtIndex:[_pickView selectedRowInComponent:1]];
        NSString *citCode = [cityCodeArray objectAtIndex:[_pickView selectedRowInComponent:1]];
        NSLog(@"+++++++%@",citCode);
        NSString *town = [townArray objectAtIndex:[_pickView selectedRowInComponent:2]];
         NSString *twnCode = [townCodeArray objectAtIndex:[_pickView selectedRowInComponent:2]];
        NSLog(@"+++++++%@",twnCode);
        _block(self.province,city,town,proCode,citCode,twnCode);
    }
    
    [self hiddenBottomView];

    
}
-(void)showBottomView{

    self.backgroundColor = [UIColor clearColor];
    [UIView animateWithDuration:0.3 animations:^{
        _bottomView.top = kScreenHeight-navigationViewHeight-pickViewViewHeight;
        self.backgroundColor = windowColor;
    } completion:^(BOOL finished) {
 

    }];
}
-(void)hiddenBottomView
{
    [UIView animateWithDuration:0.3 animations:^{
       _bottomView.top = kScreenHeight;
        self.backgroundColor = [UIColor clearColor];
    }
   completion:^(BOOL finished) {
       if (self.province == nil) {
            _block1(@"装修地址区域");
       }
      
        [self removeFromSuperview];
        
    }];
    
}


#pragma mark - UIPicker Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return 3;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    if (component == 0) {
        return provinceArray.count;
    } else if (component == 1) {
        return cityArray.count;
    } else {
        return townArray.count;
    }
}


-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *lable=[[UILabel alloc]init];
    lable.textAlignment=NSTextAlignmentCenter;
    lable.font=[UIFont systemFontOfSize:16.0f];
    if (component == 0) {
        lable.text=[provinceArray objectAtIndex:row];
    } else if (component == 1) {
        lable.text=[cityArray objectAtIndex:row];
    } else {
        lable.text=[townArray objectAtIndex:row];
    }
    return lable;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    CGFloat pickViewWidth = kScreenWidth/3;

    return pickViewWidth;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    NSLog(@"%ld",(long)component);
    if (component == 0) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"json" ofType:@"json"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        //用系统自带库进行解析
        obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (int i = 0; i < obj.count; i++) {
            NSDictionary  *dic = obj[i];
            NSLog(@"%@",provinceArray[row]);
            if ([provinceArray[row] isEqualToString:dic[@"text"]]) {
                [cityArray removeAllObjects];
                [cityCodeArray removeAllObjects];
                [townArray removeAllObjects];
                [townCodeArray removeAllObjects];
                NSArray *arr1 = dic[@"children"];
                for (int i = 0; i< arr1.count; i++) {
                    NSDictionary *dic1 = arr1[i];
                    [cityArray addObject:dic1[@"text"]];
                   [cityCodeArray addObject:dic1[@"value"]];
                    NSArray *arr2 = dic1[@"children"];
                    for (int i = 0; i <arr2.count; i++) {
                        NSDictionary *dic2 = arr2[i];
                        [townArray addObject:dic2[@"text"]];
                       [townCodeArray addObject:dic2[@"value"]];
                    }
                }
            }
            }
    }
    [pickerView selectedRowInComponent:1];
    [pickerView reloadComponent:1];
    [pickerView selectedRowInComponent:2];
    
    if (component == 1) {
        NSString *path = [[NSBundle mainBundle]pathForResource:@"json" ofType:@"json"];
        NSData *data = [NSData dataWithContentsOfFile:path];
        //用系统自带库进行解析
        obj = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        for (int i = 0; i < obj.count; i++) {
            NSDictionary  *dic = obj[i];
            NSLog(@"%@",provinceArray[row]);
            NSArray *arr1 = dic[@"children"];
            for (int i = 0; i< arr1.count; i++){
                NSDictionary *dic1 = arr1[i];
            if ([cityArray[row] isEqualToString:dic1[@"text"]]) {
                [townArray removeAllObjects];
                [townCodeArray removeAllObjects];
                [cityCodeArray addObject:dic1[@"value"]];
                NSArray *arr2 = dic1[@"children"];
                for (int i = 0; i <arr2.count; i++) {

                    NSDictionary *dic2 = arr2[i];
                    [townArray addObject:dic2[@"text"]];
                    [townCodeArray addObject:dic2[@"value"]];
                }
                
            }
            }
        
        }
        [pickerView selectRow:1 inComponent:2 animated:YES];
    }
    
    [pickerView reloadComponent:2];
    
}


@end
